<template>
  <div>
    <!-- 左侧菜单 -->
    <div class="detail_box" style="min-height: auto;min-width:auto">
      <!-- 菜单 -->
      <div class="tab_menu">
        <div>
          <span v-for="(item,index) in menus" :class="{'choose':index===tabIndex}"
                @click="chooseTab(index)">{{ item }}订单</span>
        </div>
      </div>
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">

          <div class="operate_item">
            <label>仓库：</label>
            <el-select
              filterable
              v-model="warehouseId"
              @change="getWarehouseSender"
              placeholder="请选择仓库">
              <el-option
                v-for="(item,index) in warehouseOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item" v-if="warehouseId">
            <label>配送员：</label>
            <el-select filterable clearable placeholder="请选择配送员" v-model="senderId" value-key="id">
              <el-option
                :key="item.id"
                :label="item.name"
                :value="item.id"
                v-for="(item,index) in senderList">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <label>编号：</label>
            <el-input clearable placeholder="请输入编号" type="text" v-model="keyword"></el-input>
          </div>
          <div class="operate_item">
            <label>时间：</label>
            <el-date-picker
              v-model="proDate"
              type="datetimerange"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </div>
          <div class="operate_item">
            <span @click="getTableInfo(1)" class="operate_btn search_btn">查询</span>
            <span @click="resetTable" class="operate_btn reset_btn">重置</span>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <div class="flex_box">
        <div class="detail_part" :class="{'left_box':isSee}">
          <div class="table_box">
            <el-table
              :data="tableInfo"
              :header-cell-style="tableHeader"
              border
              ref="multipleTable"
              style="width: 100%;"
              tooltip-effect="dark"
              v-loading="loading" @sort-change="getChange">
              <el-table-column
                label="ID"
                prop="id"
                width="80">
              </el-table-column>
              <el-table-column
                label="创建时间"
                prop="1"
                sortable="custom"
                width="160">
                <template slot-scope="scope">
                  <p style="text-align: left">{{ formatTime(scope.row.createTime) }}</p>
                </template>
              </el-table-column>
              <el-table-column
                width="240"
                label="订单编号">
                <template slot-scope="scope">
                  <span>{{ scope.row.deliveryNo ? scope.row.deliveryNo : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="240"
                label="回收编号">
                <template slot-scope="scope">
                  <span>{{ scope.row.recycleNo ? scope.row.recycleNo : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="150"
                sortable="custom"
                label="配送人员" prop="4">
                <template slot-scope="scope">
                  <span>{{ scope.row.senderName ? scope.row.senderName : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="120"
                sortable="custom"
                label="商品数量" prop="3">
                <template slot-scope="scope">
                  <span>{{ scope.row.numbers ? scope.row.numbers : 0 }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="150"
                label="回收空桶数量">
                <template slot-scope="scope">
                  <span>{{ scope.row.returnNumber ? scope.row.returnNumber : 0 }}</span>
                </template>
              </el-table-column>

              <el-table-column
                label="仓库">
                <template slot-scope="scope">
                  <span>{{ scope.row.warehouseName }}</span>
                </template>
              </el-table-column>

              <el-table-column
                width="100"
                sortable="custom"
                label="状态" prop="2">
                <template slot-scope="scope">
                  <span :style="'color:'+(scope.row.enable===1 ? '#4ac25e' : 'orange')">{{
                      scope.row.enable === 1 ? '已回收' : '待回收'
                    }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="180"
                fixed="right"
                v-if="btn&&(btn.detail||btn.confirm || btn.queryShipment || btn.cancel)"
                label="操作">
                <template slot-scope="scope">
                  <div class="table_btn">
                    <span @click="function(){isSee=true;orderNo=scope.row.recycleNo;}" class="change_info"
                          v-if="btn.detail">详情</span>
                    <span class="change_info success_btn" @click="openPopup(scope.row.id,scope.row.recycleNo)"
                          v-if="scope.row.enable!==1&&btn.confirm">确认</span>
                    <span @click="goShipment(scope.row)" class="change_info" v-if="btn.queryShipment">出货单</span>
                    <span @click="deleteItem(scope.row)" class="change_info delete_btn" v-if="btn.cancel">撤销</span>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="record_page">
            <span class="page_count">共<i>{{ totals }}</i>条</span>
            <el-pagination
              :current-page="currentPage"
              :page-size="pageSize"
              :page-sizes="[15, 20, 30, 40]"
              :total="totals"
              @current-change="getTableInfo"
              @size-change="sizeChange"
              background
              layout="prev, pager, next, sizes,jumper">
            </el-pagination>
          </div>
        </div>
        <div class="right_box" v-if="isSee">
          <p class="page_title">{{ orderNo }}订单详情 <i class="detail_close" @click="closeDetail"></i> </p>
          <detail :orderId="orderNo"></detail>
          <p class="page_title">{{ orderNo }}订单日志 <i class="detail_close" @click="closeDetail"></i> </p>
          <log :orderId="orderNo"></log>
        </div>
      </div>

    </div>
    <edit :mid="menuId" :rid="rid" @listenToChild="listenToEdit" v-if="isPop"></edit>
  </div>
</template>

<script>
import edit from "./edit";
import log from './log/index'
import detail from './detail/index'
import commons from "@/utils/common";
import {getBtn} from "@/utils/btnAuth";
import {getRecycleOrder,cancelRecycleOrder} from "@/api/basic/warehouse/recycleOrder";
import {getAllData} from '@/api/basic/warehouse/sender'
import {judgeRole} from "@/utils/warehouseAuth";
import {formatTree} from "@/utils/menuAuth";

export default {
  props: ['wdata'],
  components: {
    edit,
    log,
    detail
  },
  data() {
    return {
      proDate: '',
      keyword: '',
      warehouseId: this.wdata ? this.wdata.id : '',
      senderId: '',
      senderList: [],
      warehouseOption: [],
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      menuId: '',
      rid: '',
      isPop: false,
      btn: {},
      roleHouse: {},
      isSee: false,
      orderNo: '',
      sortNumber: '',
      menus: ['待回收', '已完成'],
      tabIndex: 0,
      status: -1
    }
  },
  mounted() {
    this.roleHouse = judgeRole();
    this.warehouseOption = this.roleHouse.list;
    if (this.wdata) {
      this.getWarehouseSender();
      this.keyword = this.wdata.orderNo ? this.wdata.orderNo : '';
    } else {
      this.warehouseId = this.roleHouse.id;
      if (this.warehouseId) {
        this.getWarehouseSender();
      }
    }
    if (this.$route.query.deliveryNo) {
      this.keyword = this.$route.query.deliveryNo;
    }
    if (this.$route.query.id) {
      this.warehouseId = parseFloat(this.$route.query.id);
      this.getWarehouseSender();
    }
    this.btn = getBtn();
    if (this.btn.query) {
      this.getTableInfo(1);
    }

  },
  methods: {
    //监听列表排序
    getChange(val) {
      let sort = parseInt(val.prop)
      if (this.sortNumber < 0) {
        this.sortNumber = sort;
      } else {
        this.sortNumber = -sort;
      }
      //重新获取列表
      this.getTableInfo(1);
    },

    chooseTab(index) {
      this.isSee = false;
      this.status = index === 0 ? -1 : 1;
      this.tabIndex = index;
      this.getTableInfo(1);
    },

    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        keyword: that.keyword,
        warehouseId: that.warehouseId,
        senderId: that.senderId,
        limit: that.pageSize,
        page: page,
        sort: that.sortNumber,
        enable: that.status,
        startDate: that.proDate ? that.proDate[0] : '',
        endDate: that.proDate ? that.proDate[1] : '',
      };
      getRecycleOrder(queryParams).then(res => {
        that.tableInfo = res.list ? res.list : [];
        that.totals = res.count;
        that.loading = false;
      }).catch((e) => {
        that.tableInfo = [];
        that.totals = 0;
        that.loading = false;
      });
    },

    resetTable() {
      this.keyword = '';
      this.senderId = '';
      this.pageSize = 15;
      this.sortNumber = '';
      this.proDate = '';
      this.warehouseId = this.roleHouse.id ? this.roleHouse.id : '';
      this.getTableInfo(1)
    },
    //关闭详情和日志
    closeDetail() {
      this.isSee = false;
    },
    //撤销回收单
    deleteItem(obj) {
      let that = this;
      that.$confirm('确定撤销' + obj.recycleNo + '回收单信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return cancelRecycleOrder(obj.id);
      }).then(() => {
        that.getTableInfo(that.currentPage);
        that.$message.success({
          message: '撤销成功！',
          duration: 1500
        });
      }).catch(() => {
      });
    },
    //出货单
    goShipment(obj) {
      let that = this;
      let url = '/warehouse/shipment';
      let parent = '仓库管理';
      that.$router.push({
        path: url,
        query: {
          key: formatTree(parent, url),
          recycleNo: obj.deliveryNo,
          username: obj.deliveryNo,
        }
      })
    },

    //获取仓库配送员
    getWarehouseSender() {
      let that = this;
      that.senderId = '';
      let queryParams = {
        warehouseId: that.warehouseId,
        isAll:1
      };
      getAllData(queryParams).then(res => {
        that.senderList = res ? res : [];
      }).catch((e) => {
        that.senderList = [];
      });
    },

    // 打开弹窗
    openPopup(id, rid) {
      this.isPop = true;
      this.menuId = id;
      this.rid = rid;
    },

    listenToEdit(e) {
      this.isPop = false;
      if (e.refresh) {
        this.getTableInfo(this.currentPage)
      }
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600;'
    },
  }
}
</script>

<style scoped>
.flex_box {
  display: flex;
}

.detail_part {
  width: 100%;
}

.left_box {
  width: 60%;
}

.right_box {
  width: 40%;
}

.page_title {
  font-weight: 600;
  padding-left: 20px;
  margin-bottom: 10px;
  font-size: 18px;
  position: relative;
}

</style>
